﻿'
' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2008
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports System.IO
Imports Boogaloo.Core.UI
Imports Boogaloo.Core.BLL

Namespace UI.UserControls

    Partial Public Class Host_themes
        Inherits CoreControlBase

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            If Not Page.IsPostBack Then
                lit_success.Visible = False
                Dim dirs As String() = Directory.GetDirectories(Server.MapPath("~/App_Themes/"))
                Dim dir As String = String.Empty
                For Each dir In dirs
                    Dim dirInfo As New DirectoryInfo(dir)
                    If Not ((CDbl(dirInfo.Attributes And FileAttributes.Hidden)) = FileAttributes.Hidden) Then chkl_themes.Items.Add(New ListItem(dir.Replace(Server.MapPath("~/App_Themes/"), "")))
                Next

                If chkl_themes.Items.Count = 0 Then
                    Me.Visible = False
                Else
                    BindControl()
                End If

            End If

        End Sub

        Protected Sub BindControl()
            pnl_delete.Visible = False
            Dim item As ListItem = Nothing
            Dim current As Dictionary(Of String, Theme) = Theme.GetThemesDictionary(Groove.admin_root.root_id)
            For Each item In chkl_themes.Items
                If current.ContainsKey(item.Text) Then item.Selected = True
            Next

            With lhddl_globaltheme
                .dropdownlist.DataSource = Groove.admin_root.themes
                .dropdownlist.DataTextField = "name"
                .dropdownlist.DataValueField = "theme_id"
                .dropdownlist.DataBind()
                .dropdownlist.Items.Insert(0, New ListItem("ASP.NET Themes not used", 0))


                Dim assigned_themes As List(Of Theme) = Core.BLL.Theme.GetCurrentNodeThemes(Groove.admin_root.root_id)
                If assigned_themes.Count >= 1 Then
                    If Core.BLL.Theme.AllNodesInRootHaveTheme(Groove.admin_root.root_id, assigned_themes(0).theme_id) Then
                        .dropdownlist.SelectedValue = assigned_themes(0).theme_id.ToString
                        .label = String.Format("All pages within this host are currently using the '{0}' ASP.NET Theme", assigned_themes(0).name)
                    Else
                        .dropdownlist.Items.Add(New ListItem("Mixed ASP.NET Themes", -1))
                        .dropdownlist.SelectedValue = -1
                        .label = "This host uses more than one ASP.NET Theme"
                    End If
                Else
                    .label = "(Click to globally set the theme for this host)"
                End If

            End With

        End Sub

        Protected Sub chkl_themes_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles chkl_themes.SelectedIndexChanged

            Dim chkl As CheckBoxList = CType(sender, CheckBoxList)
            Dim exist As Dictionary(Of String, Theme) = Theme.GetThemesDictionary(Groove.admin_root.root_id)

            ' figure out if this is an add or delete
            Dim item As ListItem = Nothing
            For Each item In chkl.Items
                Dim found As Boolean = False
                If Not exist.ContainsKey(item.Text) And item.Selected Then
                    ' this is a new item
                    Theme.InsertTheme(item.Text, Groove.admin_root.root_id)                
                    found = True
                    lit_success.Visible = True
                    BindControl()
                ElseIf exist.ContainsKey(item.Text) And Not item.Selected Then
                    ' this is a delete 
                    pnl_delete.Visible = True
                    lb_delete.CommandArgument = exist(item.Text).theme_id
                    hyp_cancel.NavigateUrl = Request.Url.AbsoluteUri
                    chkl_themes.Visible = False
                    found = True
                End If
                If found Then Exit For
            Next


        End Sub

        Protected Sub lb_delete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lb_delete.Click

            Dim lb As LinkButton = CType(sender, LinkButton)
            Dim theme_id As Integer = CType(lb.CommandArgument, Integer)

            Dim success As Boolean = Theme.DeleteTheme(theme_id)
            Response.Redirect(Request.Url.AbsoluteUri)

        End Sub


        Protected Sub lhddl_globaltheme_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lhddl_globaltheme.Click
            With lhddl_globaltheme.dropdownlist
                If CType(.SelectedValue, Integer) >= 0 Then Groove.admin_root.UpdateAllRootNodesTheme(CType(.SelectedValue, Integer))
            End With
            BindControl()
        End Sub

    End Class

End Namespace